function Promise(executor){
    this.PromiseState = 'pending';
    this.PromiseResult = undefined;
    this.callbacks = [];
    const resolve = (value)=>{
        if(this.PromiseState !== 'pending') return;
        this.PromiseState = 'fulfilled';
        this.PromiseResult = value;
        if(this.callbacks.length){
            this.callbacks.forEach(item=>{
                item.success();
            })
        }
    }
    const reject = (reason)=>{
        if(this.PromiseState !== 'pending') return;
        this.PromiseState = 'rejected';
        this.PromiseResult = reason;
        if(this.callbacks.length){
            this.callbacks.forEach(item=>{
                item.fail();
            })
        }
    }
    try{
        executor(resolve, reject);
    }catch(e){
        reject(e);
    }
}

Promise.prototype.then = function(onResolve, onReject){
    if(this.PromiseState === 'fulfilled'){
        onResolve(this.PromiseResult);
    }
    if(this.PromiseState === 'rejected'){
        onReject(this.PromiseResult);
    }
    if(this.PromiseState === 'pending'){
        this.callbacks.push({
            success:()=>{
                onResolve(this.PromiseResult);
            },
            fail:()=>{
                onReject(this.PromiseResult);
            }
        })
    }
}